import {
  debounce as debounceFunc,
  throttle as throttleFunc,
  DebounceSettings,
  ThrottleSettings,
} from 'lodash';

export * from './method';

export function debounce(wait?: number, options?: DebounceSettings) {
  return function debounceWrapper(object: any, key: any, descriptor: any) {
    const func = object[key];
    if (typeof func !== 'function') {
      throw new Error('只能装饰方法');
    }
    descriptor.value = debounceFunc(func, wait, options);
    return descriptor;
  };
}

export function throttle(wait?: number, options?: ThrottleSettings) {
  return function throttleWrapper(object: any, key: any, descriptor: any) {
    const func = object[key];
    if (typeof func !== 'function') {
      throw new Error('只能装饰方法');
    }
    descriptor.value = throttleFunc(func, wait, options);
    return descriptor;
  };
}
